package cn.hyy.FVOnline.mapper;

import cn.hyy.FVOnline.entity.Message;
import cn.hyy.FVOnline.entity.Order;
import org.apache.ibatis.annotations.*;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("orderMapper")
public interface OrderMapper {


    @Results(id = "orderMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "uId", column = "uid"),
            @Result(property = "goodsId",column = "goodsid"),
            @Result(property = "shopId",column = "shopid"),
            @Result(property = "addressId",column = "addressid"),
            @Result(property = "tradeNum",column = "tradenum"),
            @Result(property = "tradePrice",column = "tradeprice"),
            @Result(property = "expressName",column = "expressname"),
            @Result(property = "orderNumber",column = "ordernumber"),
            @Result(property = "expressNumber",column = "expressnumber"),
            @Result(property = "remarks",column = "remarks"),
            @Result(property = "createTime",column = "cteatetime"),
            @Result(property = "payTime",column = "paytime"),
            @Result(property = "sendTime",column = "sendtime"),
            @Result(property = "state",column = "state")
    })
    @Select("select * from fv_order where id = #{id}")
    Order findById(Integer id);


    @Insert("insert into fv_order (uid,goodsid,shopid,addressid,tradenum,tradeprice,ordernumber,remarks,cteatetime,paytime,state) values (" +
            "#{order.uId}, #{order.goodsId}, #{order.shopId}, #{order.addressId}, #{order.tradeNum}, #{order.tradePrice}, #{order.orderNumber}, " +
            "#{order.remarks}, #{order.createTime}, #{order.payTime}, #{order.state})")
    @Options(useGeneratedKeys = true,keyProperty = "order.id")
    void createOrder(@Param("order") Order order);

    @Select("select * from fv_order where shopid = #{shopid} and state = 1 limit #{start}, #{size}")
    List<Order> findByShopId(@Param("shopid") Integer shopid,@Param("start")Integer start,@Param("size")Integer size);

    @Select("select count(*) from fv_order where shopid = #{shopid} and state = 1")
    Integer findShopTotal(@Param("shopid") Integer shopid);

    @Update("update fv_order set expressname = #{order.expressName}, expressnumber = #{order.expressNumber}, " +
            "state = #{order.state}, sendtime = #{order.sendTime} where id = #{order.id}")
    void sendExpress(@Param("order") Order order);

    @Insert("insert into message (onum,uid) values (#{ordernum}, #{uid})")
    void addMessage(@Param("ordernum")String ordernum,@Param("uid")Integer uid);

    @Select("select * from message where uid = #{uid}")
    List<Message> getMessage(@Param("uid")Integer uid);

    @Delete("delete from message where uid = #{uid}")
    void delMessage(@Param("uid")Integer uid);

    @Select("select * from fv_order where shopid = #{shopid} and state = 2 limit #{start}, #{size}")
    List<Order> findByShopIdTwo(@Param("shopid") Integer shopid,@Param("start")Integer start,@Param("size")Integer size);

    @Select("select count(*) from fv_order where shopid = #{shopid} and state = 2")
    Integer findShopTotalTwo(@Param("shopid") Integer shopid);

    @ResultMap("orderMap")
    @Select("select * from fv_order where uid = #{userid} limit #{start},#{size}")
    List<Order> findUserOrder(@Param("userid")Integer userid,@Param("start")Integer start,@Param("size")Integer size);

    @Select("select count(*) from fv_order where uid = #{userid}")
    Integer findUserTotal(@Param("userid") Integer userid);

    @Update("update fv_order set state = 3 where id = #{id}")
    void receive(@Param("id")Integer id);

    @ResultMap("orderMap")
    @Select("select * from fv_order where id = #{id}")
    Order findByOrderId(@Param("id")Integer id);

    @Update("update fv_order set state = 4 where id = #{id}")
    void evaluate(@Param("id")Integer id);

    @ResultMap("orderMap")
    @Select("select * from fv_order where ordernumber like #{what} limit #{start}, #{size}")
    List<Order> getOrder(@Param("what")String what,@Param("start")Integer start,@Param("size")Integer size);

    @Select("select count(*) from fv_order where ordernumber like #{what}")
    Integer getOrderTotal(@Param("what")String what);
}
